import unittest
import requests
import time
from common import public_login

nowtime = time.strftime("%Y%m%d%H%M%S")
one_time = time.strftime('%Y-%m-%d %H%M%S')
two_time = time.strftime('%Y-%m-%d')

base_header = {'cookie': public_login.gl_cookie()}


class MyTestCase(unittest.TestCase):

    def setUp(self):
        print("Start the use case ！")

    def tearDown(self):
        print(self.result)
        print("Use case execution ends ！")

    """
    接口请求方法，
    封装默认检查"status"等于"01"，
    调用时可自己定义别的检查点，
    也可不输入直接默认。
    """
    def base_req(self, base_url, base_data, base_Checkpoint="status", base_Checkchar="01"):
        r = requests.post(url=base_url, data=base_data, headers=base_header)
        self.result = r.json()
        res = str(self.result)
        if len(res) > 50:
            """
            assertIn 判断是否包含, assertEqual 判断是否一致
            """
            # self.assertIn(str('"status":"01"'), res)
            result = eval(self.result['result'])
            self.assertEqual(result[base_Checkpoint], base_Checkchar)
        else:
            self.assertEqual(self.result['result'], '成功')
            # self.assertIn("'result': '成功'", res)

        return self.result

    def test_home_rate(self):
        base_url = 'http://plsadmpre.cnsuning.com/plsadm/loan/test/qryLoanRate.do'
        base_data = {'partnerNo': '10', 'intfNo': 'A016', 'productNo': 'RXD', 'custSource': '01',
                     'idNo': '520101198708150259'}
        # base_Checkpoint = 'status'
        # base_Checkchar = '01'
        self.base_req(base_url, base_data)

    def test_yuqil_job(self):
        base_url = 'http://plsadmPre.cnsuning.com/plsadm/loan/test/job.do'
        base_data = {}
        base_Checkpoint = 'result'
        base_Checkchar = '成功'
        self.base_req(base_url, base_data, base_Checkpoint, base_Checkchar)

    def test_qb_xysq(self):
        base_url = 'http://plsadmpre.cnsuning.com/plsadm/http/callBackQuotaAppl.do'
        base_data = {"serialNo": nowtime, 'transNo': nowtime, 'timestamp': nowtime,
                     'custName': '孙芸筱', 'idNo': '130101198712040017', "addressDetail": '苏宁大道3号',
                     "appCode": '70058302', 'app_id': 'yfbm70067441e2017120801', 'bizCode': '02',
                     'companyName': '苏宁有限公司', 'consumeDesc': '消费说明', 'contactAddress': '苏宁大道1号',
                     'contactPostCode': '210001', 'cooperativeAgency': '合作机构名称',
                     'custMobile': '18551631867', 'guaranteeType': '02', 'houseType': '1', 'idType': '01',
                     'industryType': 'T', 'intfNo': 'A0001', 'marryType': '2', 'matesCompany': '苏宁体育',
                     'matesIdNo': '320830198809180812', 'matesIdType': '01', 'matesName': '筱筱',
                     'matesPhone': '15600000000', 'maxDegree': '01', 'maxEducation': '01', 'occupation': '0',
                     'partnerNo': '15', 'payWay': 'R9929', 'plDescribe': '押品说明', 'pledgeType': '02',
                     'postCode': '210003',
                     'productNo': 'RXD', 'projectCode': '110', 'quotaRate': '0.000167', 'quotaStyle': '02',
                     'quotaTerm': '2', 'quotaType': '04', 'quotaUse': 'COS', 'quotaUseDesc': '买买买',
                     'server_address': 'https://fopenapipre.cnsuning.com:8443/gateway.htm',
                     'service': 'suning.fosps.pls.ExecuteRemotePlsadmPlatformServer.quotaApplyCheck',
                     'sign': '后台代码实现', 'version': '1.0',
                     'sign_type': 'RSA2', 'signkey_index': '0001', 'termType': '02',
                     'uploadList[0].authDate': '20180224:084654', 'uploadList[0].uploadFileType': '15',
                     'uploadList[0].uploadNo': 'FOS2017111500002140', 'uploadList[0].uploadResult': '1',
                     'workAddress': '苏宁大道2号', 'workDuties': '1', 'workDutiesName': '1', 'workPostCode': '210001'}
        base_Checkpoint = 'status'
        base_Checkchar = '02'
        self.base_req(base_url, base_data, base_Checkpoint, base_Checkchar)

    def test_smd_yxsq(self):
        base_url = 'http://plsadmpre.cnsuning.com/plsadm/loan/test/confirmCommitLoan.do'
        base_data = {'paySerialNo': nowtime, 'payOrderId': nowtime, 'applyTime': one_time, 'signCode': two_time,
                     'eppAccountId': '0000000001118269483', 'idNo': '520101198708150259', 'applyAmount': '600',
                     'repayWay': 'R9925', 'loanTerm': '6', 'personLoanId': '21966406', 'projectCode': '204',
                     'partnerNo': '10', 'intfNo': 'Q009', 'quotaType': '02', 'termType': '02'}
        base_Checkpoint = 'status'
        base_Checkchar = '02'
        self.base_req(base_url, base_data, base_Checkpoint, base_Checkchar)


if __name__ == '__main__':
    unittest.main()
